package com.zuofei.xmpp.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.elvishew.xlog.XLog;
import com.zuofei.xmpp.dbhelper.ContactOpenHelper;

public class ContactsProvider extends ContentProvider {

    public static final String AUTHORITIES = ContactsProvider.class.getCanonicalName();//得到类的完整路径
    static UriMatcher mUriMathcher;
    private static final int CONTACT =  1;
    //对应联系人表的一个uri常量
    public static Uri URI_CONTACT = Uri.parse("content://"+AUTHORITIES+"/contact");
    static {
        mUriMathcher = new UriMatcher(UriMatcher.NO_MATCH);
        //添加一个匹配规则
        mUriMathcher.addURI(AUTHORITIES,"/contact",CONTACT);
        //content://com.zuofei.xmpp.provider.ContactsProvider/contact-->CONTACT
    }

    private ContactOpenHelper mHelper;

    @Override
    public boolean onCreate() {
        mHelper = new ContactOpenHelper(getContext());
        if(mHelper != null){
            return true;
        }
        return false;
    }

    @Override
    public String getType( Uri uri) {
        return null;
    }


    @Override
    public Uri insert( Uri uri,  ContentValues values) {
        //数据是存到sqlite->创建db文件,建立表-->sqliteOpenHelper
        int code = mUriMathcher.match(uri);
        switch (code){
            case CONTACT:
                SQLiteDatabase db =  mHelper.getWritableDatabase();
                long id = db.insert(ContactOpenHelper.T_CONTACT,"",values);
                if(id != -1){
                    XLog.d("-------------------ContactsProvider------insertSuccess");
                    //拼接最新的uri
                    uri =  ContentUris.withAppendedId(uri,id);
                    //通知ContentObserver数据改变了
                    getContext().getContentResolver().notifyChange(ContactsProvider.URI_CONTACT,null);
                }
                break;
                default:
                    break;
        }
        return null;
    }

    @Override
    public int delete( Uri uri,  String selection,  String[] selectionArgs) {
        int code = mUriMathcher.match(uri);
        int deleteCount = 0;
        switch (code){
            case CONTACT:
                SQLiteDatabase db =  mHelper.getWritableDatabase();
                //影响的行数
                deleteCount = db.delete(ContactOpenHelper.T_CONTACT,selection,selectionArgs);
                if(deleteCount > 0){
                    XLog.d("删除成功");
                    getContext().getContentResolver().notifyChange(ContactsProvider.URI_CONTACT,null);
                }
                break;
            default:
                break;
        }
        return deleteCount;
    }

    @Override
    public int update( Uri uri,  ContentValues values,  String selection,  String[] selectionArgs) {
        int code = mUriMathcher.match(uri);
        int updateCount = 0;
        switch (code){
            case CONTACT:
                SQLiteDatabase db =  mHelper.getWritableDatabase();
                updateCount =  db.update(ContactOpenHelper.T_CONTACT,values,selection,selectionArgs);
                if(updateCount > 0){
                    XLog.d("更新成功");
                    getContext().getContentResolver().notifyChange(ContactsProvider.URI_CONTACT,null);
                }
                break;
            default:
                break;
        }
        return updateCount;
    }
    @Override
    public Cursor query( Uri uri, String[] projection,  String selection,  String[] selectionArgs, String sortOrder) {
        int code = mUriMathcher.match(uri);
        Cursor cursor = null;
        switch (code){
            case CONTACT:
                SQLiteDatabase db =  mHelper.getWritableDatabase();
                cursor = db.query(ContactOpenHelper.T_CONTACT,projection,selection,selectionArgs,null,null,sortOrder);
                XLog.d("查询成功");
                break;
            default:
                break;
        }
        return cursor;
    }
}
